package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.entity.PageReq;
import com.itheima.entity.PageRes;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckGroupService;
import com.itheima.vo.CheckGroupVO;
import com.itheima.vo.SetmealVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {
    @Resource
    private CheckGroupItemService checkGroupItemService;
    //根据id删除记录
    @Override
    public boolean deleteById(Long id) {
      CheckGroup checkGroup =  new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setIs_delete(1);
        this.updateById(checkGroup);
        return true;
    }
    //前端页面返回信息新增
    @Override
    @Transactional
    public boolean saveGroup(CheckGroupDto checkGroupDto) {
        //保存检查组信息
        this.saveOrUpdate(checkGroupDto.getCheckGroup());
        //清除之前维护的关系数据
        checkGroupItemService.deleteByGroupId(checkGroupDto.getCheckGroup().getId());
        //维护检查组与检查项的关系
        if(checkGroupDto.getItemIds() != null){
            //批处理集合
            List<CheckGroupItem> list = new ArrayList<>();
            for(Long itemId : checkGroupDto.getItemIds()){
                CheckGroupItem checkGroupItem =  new CheckGroupItem();
                checkGroupItem.setGroupId(checkGroupDto.getCheckGroup().getId());
                checkGroupItem.setItemId(itemId);
//                checkGroupItemService.saveOrUpdate(checkGroupItem);//逐条插入
                list.add(checkGroupItem);
            }
            checkGroupItemService.saveBatch(list);//批量插入
        }
        return true;
    }
    //分页查询
    public PageRes<CheckGroup> findByPage(PageReq req){
        //条件构造器
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        String querySting = req.getQueryString();
        queryWrapper.eq("is_delete", 0);
        if(querySting != null && "".equals(querySting.trim()) == false){
            //拼接
            queryWrapper.and(Wrapper -> {
                Wrapper.like("name", req.getQueryString()).or()
                        .like("code", req.getQueryString()).or()
                        .like("help_code", req.getQueryString());
            });
        }
        queryWrapper.orderByDesc("id");
        IPage<CheckGroup> result = this.page(new Page(req.getCurrentPage(), req.getPageSize()), queryWrapper);
        //分页结果返回到前端list
        return new PageRes<CheckGroup>(result.getTotal(),result.getRecords());

    }

    @Override
    public List<CheckGroupVO> selectBySetmealId(long setmealId) {
        return this.baseMapper.selectBySetmealId(setmealId);
    }
}
